"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from store.models import Cart
from cart.views import get_cart
from django.test import TestCase
from django.test import Client
import unittest
from django.utils.datetime_safe import datetime
from django.contrib.admin.models import User
from pdb import Pdb


from django.contrib.auth.models import AnonymousUser
from django.test.client import RequestFactory
class GetCartTest5(unittest.TestCase):
    def setUp(self):
        self.rf=RequestFactory()
        self.client=Client()
    def test(self):
        c=Client()
        #request=MyRequest2(c,None)
        
        request=self.rf.get('/someshit/')
        request.user=AnonymousUser()
        request.session={}
        request.session['someshit']=1
        cart=get_cart(request)
        assert cart.pk==request.session['cart']==cart.pk
        assert cart.user == None
        self.delete()
    def delete(self):
        User.objects.all().delete()
        Cart.objects.all().delete()
        
class GetCartTest4(unittest.TestCase):
    def setUp(self):    
        self.cart1=Cart()
        self.cart1.date=datetime(year=2011,month=10,day=29,hour=12,minute=12,second=12)
        self.cart1.save()
        self.rf=RequestFactory()
    def test(self):
        # request two is a object who's fields are a map called "session" and the django.contrib.auth.models.Anomnymous.User called "user"
        request=MyRequest2(AnonymousUser(),self.cart1)
        cart=get_cart(request)
        assert cart.pk==self.cart1.pk
        assert cart.user == None
        self.delete()
    def delete(self):
        pass
        User.objects.all().delete()
        Cart.objects.all().delete()
        
class GetCartTest3(unittest.TestCase):
    def setUp(self):
        self.c=Client()
        self.user=User.objects.create_user('jenia23456','jenia20093@yahoo.com','2345')
        self.c.login(username='jenia23456',password='2345')
    def test(self):
        cart=get_cart(MyRequest1(self.user))
        assert cart.pk==Cart.objects.get(user=self.user).pk
        assert self.user.is_authenticated()
        self.delete()
    def delete(self):
        self.c.logout()
        User.objects.all().delete()
        Cart.objects.all().delete()
       
class GetCartTest2(unittest.TestCase):
      #===========================================================================
    # User is logged in and has one cart
    # MUST RETURN USER'S CART
    # MUST BE LOGGED IN
    # USER HAS CART 1
    #===========================================================================
    def setUp(self):
        date1=datetime(year=2011,month=10,day=29,hour=12,minute=12,second=12)
        self.c=Client()
        self.user=User.objects.create_user('jenia2345','jenia2009@yahoo.com','2345')
        self.c.login(username='jenia2345',password='2345')
        self.cart1=Cart(date=date1)
        self.cart1.user=self.user
        self.cart1.save()
    # The user is logged in, the method has returned cart 1 and the user has cart 1
    def test(self):
        cart=get_cart(MyRequest1(self.user))
        assert cart.pk==self.cart1.pk
        assert self.user.is_authenticated()
        assert self.user.cart_set.all()[0].pk==self.cart1.pk
        self.delete()
    def delete(self):
        self.c.logout()
        User.objects.all().delete()
        Cart.objects.all().delete()
        
class GetCartTest1(unittest.TestCase):
    #===========================================================================
    # The user is logged in.
    # He has two cart
    # SUPPOSE TO RETURN THE LASTEST CART
    # USER MUST BE LOGGED IN
    # THE USER HAS CART 2
    #===========================================================================
    def setUp(self):
        user=User.objects.create_user('jenia2345','jenia2009@yahoo.com','2345')
        self.user=user
        self.c=Client()
        self.c.login(username='jenia2345', password='2345')
        date1=datetime(year=2011,month=10,day=29,hour=12,minute=12,second=12)
        date2=datetime(year=2011,month=10,day=30,hour=12,minute=12,second=12)
        self.cart1=Cart(date=date1)
        self.cart2=Cart(date=date2)
        self.cart1.user=user
        self.cart2.user=user
        self.cart1.save()
        self.cart2.save()
    #=======================================================================
    # The user is logged in, the method has returned cart 2 and the user has cart 2
    #=======================================================================
    def test(self):
        my_request=MyRequest1(self.user)
        cart=get_cart(my_request)
        assert cart.pk==self.cart2.pk
        assert self.user.is_authenticated()
        assert self.user.cart_set.all()[0].pk==self.cart1.pk
        assert self.user.cart_set.all()[1].pk==self.cart2.pk
        self.delete()
    def delete(self):
        self.c.logout()
        User.objects.all().delete()
        Cart.objects.all().delete()
        
class MyRequest1():
    def __init__(self,user):
        self.user=user
        self.cart1=Cart()
        
#===============================================================================
# The user is anonymous
#===============================================================================
from django.http import HttpRequest
class MyRequest2():
    
    def __init__(self,user,cart):
        self.user=user
        self.session={'cart':cart.pk}
